package com.sise.aio和bio和nio.nio.基于nio的一个通信案例;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

/**
 * 没有选择器的demo
 * @author idea
 * @data 2019/1/20
 */
public class NioServer implements Runnable {

    @Override
    public void run() {
        try {
            ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
            ServerSocket socket = serverSocketChannel.socket();
            socket.bind(new InetSocketAddress(9999));
            serverSocketChannel.configureBlocking(true);
            ByteBuffer buffer = ByteBuffer.allocate(1024);

            SocketChannel client = serverSocketChannel.accept();
            while (true) {
                int len = client.read(buffer);
                if(len>0){
                    System.out.println("客户端信息："+new String(buffer.array(),0,len));
                }
                buffer.clear();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
